package com.woorigil.social.domain.deal;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.woorigil.social.HttpUtils;
import com.woorigil.social.domain.ProviderCode;

@Repository
public class CoupangMainDealDao implements MainDealDao {
	String mainUrl = "http://www.coupang.com/main.pang";

	@Override
	public List<MainDealMeta> findMainDeals(int size) {
		String html = HttpUtils.getHtml(mainUrl);
		Pattern p = Pattern.compile("(/deal.pang\\?coupang=([0-9]*)&areacode=MB)");
		Matcher m = p.matcher(html);
		
		List<MainDealMeta> mainDealMetas = new ArrayList<MainDealMeta>();
		List<String> metaIds = new ArrayList<String>();
		while (m.find()) {
			String metaId = StringUtils.trim(m.group(2));
			if (!metaIds.contains(metaId)) {
				mainDealMetas.add(new MainDealMeta(ProviderCode.CP, metaId));
				metaIds.add(metaId);
			}
		}
		return mainDealMetas;
	}
}
